package com.jtjrenren.android.taxi.driver.core.wallect;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


import com.jtjrenren.android.taxi.driver.Constants;
import com.jtjrenren.android.taxi.driver.R;
import com.jtjrenren.android.taxi.driver.bean.DriverInfo;
import com.jtjrenren.android.taxi.driver.manger.AppManger;

import com.jtjrenren.android.taxi.driver.bean.api.ApiBindPayResult;
import com.jtjrenren.android.taxi.driver.bean.api.ApiCommonResult;
import com.jtjrenren.android.taxi.driver.base.BaseActivity;
import com.jtjrenren.android.taxi.driver.manger.api.ApiWorks;
import com.library.weiget.ClearEditText;
import com.library.utils.StringUtils;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnTextChanged;

/**
 * Created by Administrator on 2016/5/6.
 */
public class WallectTxActivity extends BaseActivity {
    @BindView(R.id.wallect_tx_name)
    EditText et_name;
    @BindView(R.id.wallect_tx_account)
    EditText et_account;
    @BindView(R.id.wallect_tx_txje)
    ClearEditText et_count;
    @BindView(R.id.wallect_tx_account_desc)
    TextView tv_accountdesc;
    @BindView(R.id.wallect_tx_bank)
    TextView tv_bank;
    @BindView(R.id.wallect_tx_qbye)
    TextView tv_ye;
    @BindView(R.id.wallect_tx_qrtx)
    Button bt_qrtx;

    private int current_type = Constants.TYPE_PAY_YHK;
    private DriverInfo driver;
    private boolean isBind = false;


    @Override
    public int getLayoutID() {
        return R.layout.aty_wallect_tixian;
    }

    @Override
    public void init() {
        if (getIntent().hasExtra("type")) {
            current_type = getIntent().getIntExtra("type", Constants.TYPE_PAY_YHK);
        }
        driver = AppManger.getInstance().driver;

        ((TextView) findViewById(R.id.title_title)).setText("提现");
        findViewById(R.id.title_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        if (driver != null) {
            et_name.setText(driver.getName());
            tv_ye.setText(driver.getRMBMoney());
        }

        if (current_type == Constants.TYPE_PAY_YHK) {
            tv_accountdesc.setText(getString(R.string.yinhangkahao));
        } else if (current_type == Constants.TYPE_PAY_ZFB) {
            tv_accountdesc.setText(getString(R.string.zhifubaozhanghao));
        }

        refreshBindMsg();
    }

    @OnClick(R.id.wallect_tx_qrtx)
    public void doTx() {
        String value = et_count.getText().toString().trim();
        String type = "1"; //默认支付宝
        if (current_type == Constants.TYPE_PAY_YHK)
            type = "4";
        String account = et_account.getText().toString().trim();

        if (StringUtils.isEmpty(account)) {
            displayShort("请首先绑定！");
            return;
        }

        if (StringUtils.isEmpty(value)) {
            displayShort(getString(R.string.tixianjineweikongqingchognxinshuru));
            return;
        }
        double txje = Double.parseDouble(value);
        double je = 0;
        if (driver != null)
            je = Double.parseDouble(driver.getRMBMoney());
        if (Double.parseDouble(value) <= 0 || txje > je) {
            displayShort(getString(R.string.shurujinebuzhegnque));
            return;
        }

        if (driver == null)
            return;
        showProDialog("", getString(R.string.shujujiazaizhongqingshaohou));
        bt_qrtx.setEnabled(false);
        ApiWorks.updateRechareMent_M(driver.getWorkNo(), type, account, value, new ApiWorks.ResponseListener<ApiCommonResult>() {
            @Override
            public void onResponse(ApiCommonResult apiCommonResult) {
                bt_qrtx.setEnabled(true);
                disProDialog();

                String errMsg = getString(R.string.tixianshibai);
                if (apiCommonResult != null && apiCommonResult.getCode() == 1) {
                    displayShort(getString(R.string.tixianchenggong));
                    finish();
                    return;
                } else
                    errMsg = apiCommonResult.getDesc();
                displayShort(errMsg);
            }
        });
    }

    private void refreshBindMsg() {
        if (driver == null)
            return;
        showProDialog("", getString(R.string.shujujiazaizhongqingshaohou));
        ApiWorks.getPayBindingData(driver.getWorkNo(), current_type, new ApiWorks.ResponseListener<ApiBindPayResult>() {
            @Override
            public void onResponse(ApiBindPayResult apiBindPayResult) {
                disProDialog();
                if (apiBindPayResult != null && !StringUtils.isEmpty(apiBindPayResult.getAccount())) {
                    //已经绑定过当前方式
                    refreshView(apiBindPayResult);
                } else {//当前方式还没有绑定过，跳转到绑定界面
                    if (isBind)
                        return;
                    isBind = true;
                    displayShort(getString(R.string.qignxianbagnding));
                    if (current_type == Constants.TYPE_PAY_YHK)
                        startActivityForResult(WallectBindPayActivity.getDiyIntent(WallectTxActivity.this, Constants.TYPE_PAY_YHK), 120);
//                        IntentUtils.goBindPayPage(WallectTxActivity.this, Constants.TYPE_PAY_YHK);
                    else if (current_type == Constants.TYPE_PAY_ZFB)
                        startActivityForResult(WallectBindPayActivity.getDiyIntent(WallectTxActivity.this, Constants.TYPE_PAY_ZFB), 120);
//                    IntentUtils.goBindPayPage(WallectTxActivity.this, Constants.TYPE_PAY_ZFB);
                }
            }
        });
    }

    private void refreshView(ApiBindPayResult obj) {
        et_account.setText(obj.getAccount());
        tv_bank.setText(obj.getBankCode());
    }

    @OnTextChanged(R.id.wallect_tx_txje)
    void onTextChanged(CharSequence text) {
        String je = text.toString().trim();
        if (je.indexOf(".") == 0) {
            et_count.setText("0" + je);
            et_count.setSelection(je.length() + 1);
        }
        if (je.length() > 1 && je.indexOf("0") == 0 && je.indexOf(".") != 1) {
            String j = je.substring(1);
            et_count.setText(j);
            et_count.setSelection(1);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==120){
            if (resultCode==RESULT_OK){
                refreshBindMsg();
            }else
                finish();
        }
    }
}
